package com.chinawlx.wlxteacher.ui.fragment;


import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.bean.WLXScheduleChangeBean;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXDateUtil;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.wlx_schedule_change;
import com.chinawlx.wlxteacher.wlx_schedule_changeDao;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by wlx on 16/10/15.
 */

public class WLXScheduleStateFragment extends DialogFragment {
    @BindView(R.id.iv_cha)
    ImageView chaImage;
    @BindView(R.id.tv_clazz_title)
    TextView clazzTitle;
    @BindView(R.id.tv_addorstop)
    TextView addOrStop;                 //加课/停课学生
    @BindView(R.id.tv_addorstop_count)
    TextView addOrStopCount;            //加课/停课次数
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.tv_date_leave)
    TextView leaveDate;                 //请假时间
    @BindView(R.id.tv_old_data_adjust)
    TextView adjustOldDate;             //调课前时间or加课开始时间or停课开始时间
    @BindView(R.id.tv_now_data_adjust)
    TextView adjustNowDate;             //调课后时间or加课结束时间or停课结束时间

    @BindView(R.id.cause)
    TextView cause;
    @BindView(R.id.tv_cause)
    TextView causeTv;
    //    private static WLXScheduleStateFragment stateFragment=new WLXScheduleStateFragment();
    private static WLXScheduleStateFragment stateFragment;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {

//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 设置背景为透明

        View view = inflater.inflate(R.layout.fragment_dialog_state, null);
        ButterKnife.bind(this, view);
        init();
        return view;
    }


    private void init() {
//        String id = getArguments().getString(WLXTeacherConstant.ID);
//        requestNetForScheduleChange(id);
//        String typeKey = getArguments().getString(WLXTeacherConstant.TYPE_KEY);
//        String reasonKey = getArguments().getString(WLXTeacherConstant.REASON_KEY);
//        WLXLogUtil.printE("id: " + id );
        clazzTitle.setText(title);
        switch (typeKey) {
            case "leave":
                leaveDate.setVisibility(View.VISIBLE);
                addOrStop.setVisibility(View.GONE);
                adjustOldDate.setVisibility(View.GONE);
                adjustNowDate.setVisibility(View.GONE);
                addOrStopCount.setVisibility(View.GONE);
                time.setText("请假时间");
                cause.setText("请假原因");
                String startdata = WLXDateUtil.getDateToString(startDate);
                String startTime = startdata.substring(0,startdata.lastIndexOf(":"));
                String endData = WLXDateUtil.getDateToString(endDate);
                String endTime = endData.substring(endData.lastIndexOf(" ") + 1, endData
                        .lastIndexOf(":"));
                leaveDate.setText(startTime+"-"+endTime);
                switch (reasonKey) {
                    case "study":
                        causeTv.setText("学习考试");
                        break;
                }
                break;
            case "adjust":
                leaveDate.setVisibility(View.GONE);
                addOrStop.setVisibility(View.GONE);
                addOrStopCount.setVisibility(View.GONE);
                adjustOldDate.setVisibility(View.VISIBLE);
                adjustNowDate.setVisibility(View.VISIBLE);
                time.setText("调课时间");
                cause.setText("调课原因");
                SimpleDateFormat format = new SimpleDateFormat("yyyy.M-d");
                adjustOldDate.setText("原时间:  "+format.format(startDate * 1000L));
                adjustNowDate.setText("现时间:  "+format.format(newStartdate * 1000L));
                break;
            case "stop":
                addOrStop.setVisibility(View.GONE);
                leaveDate.setVisibility(View.GONE);
                adjustOldDate.setVisibility(View.VISIBLE);
                adjustNowDate.setVisibility(View.VISIBLE);
                addOrStopCount.setVisibility(View.VISIBLE);
                time.setText("停课日期");
                cause.setText("停课原因");
                addOrStopCount.setText("停课次数");

                break;
            case "addition":
                addOrStop.setVisibility(View.GONE);
                leaveDate.setVisibility(View.GONE);
                adjustOldDate.setVisibility(View.VISIBLE);
                adjustNowDate.setVisibility(View.VISIBLE);
                addOrStopCount.setVisibility(View.VISIBLE);
                time.setText("加课日期");
                cause.setText("加课时间");
                addOrStopCount.setText("加课次数");
                break;

        }
    }

    private String title;
    private String typeKey;
    private String reasonKey;
    private long startDate;
    private long endDate;
    private long newStartdate;


    public void setGradeTitle(String title) {
        this.title = title;
    }

    public void setTypeKey(String typeKey) {
        this.typeKey = typeKey;
    }

    public void setReasonKey(String reasonKey) {
        this.reasonKey = reasonKey;
    }

    public void setStartDate(long startdate) {
        this.startDate = startdate;
    }
    public void setNewStartDate(long newStartdate) {
        this.newStartdate = newStartdate;
    }

    public void setEndDate(long endDate) {
        this.endDate = endDate;
    }


    public static WLXScheduleStateFragment getInstance() {
        stateFragment = new WLXScheduleStateFragment();
//        Bundle bundle=new Bundle();
//        bundle.putString(WLXTeacherConstant.ID,id);
//        bundle.putString(WLXTeacherConstant.REASON_KEY,reasonKey);
//        stateFragment.setArguments(bundle);
        return stateFragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                    .LayoutParams.MATCH_PARENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    }

    @OnClick({R.id.iv_cha})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cha:
                stateFragment.dismiss();
                break;
        }
    }

    /**
     * 请求课程改变的数据
     *
     * @param teacherChangeId
     */
    private void requestNetForScheduleChange(String teacherChangeId) {
        WLXHttpRxHelper.getInstance().getScheduleChange(teacherChangeId, new
                Subscriber<WLXScheduleChangeBean>() {


                    @Override
                    public void onCompleted() {
                        wlx_schedule_change scheduleChange = WLXGreenDaoUtil.getScheduleChangeDao()
                                .queryBuilder().where
                                        (wlx_schedule_changeDao.Properties.User_id.eq(WLXConstant
                                                .USER_ID)).unique();
                        String reason_code = scheduleChange.getReason_code();
                        String type_code = scheduleChange.getType_code();
                        Long start_date = scheduleChange.getStart_date();
                        Long end_date = scheduleChange.getEnd_date();

//                        WLXScheduleStateFragment stateFragment = WLXScheduleStateFragment
//                                .getInstance();
//                        stateFragment.setTypeKey(type_code);
//                        stateFragment.setReasonKey(reason_code);
//                        stateFragment.setStartDate(start_date);
//                        stateFragment.setEndDate(end_date);
//                        stateFragment.show(mFragmentManager, "");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(WLXScheduleChangeBean changeBean) {
                        if (changeBean.getCode() == 0) {
                            WLXScheduleChangeBean.DataBean dataBean = changeBean.getData();
                            String schedule_change_id = Integer.toString(dataBean.getId());
                            int user_id = dataBean.getUser_id();
                            int grade_id = dataBean.getGrade_id();
                            String schedule_ids = dataBean.getSchedule_ids();
                            int is_teacher = dataBean.isIs_teacher() == true ? 1 : 0;
                            String reason_code = dataBean.getReason().getKey();
                            String type_code = dataBean.getType().getKey();
                            String remark = dataBean.getRemark();
                            String extend_info = new Gson().toJson(dataBean.getExtend());
                            long start_date = WLXDateUtil.getStringToDate(dataBean.getStart_date());
                            long end_date = WLXDateUtil.getStringToDate(dataBean.getEnd_date());
                            long new_start_date = WLXDateUtil.getStringToDate(dataBean
                                    .getNew_start_date());
                            long new_end_date = WLXDateUtil.getStringToDate(dataBean
                                    .getNew_end_date());
                            long last_modified_date = WLXDateUtil.getStringToDate(dataBean
                                    .getLast_modified_date());

                            wlx_schedule_change wlxSchuduleChange = new wlx_schedule_change
                                    (schedule_change_id, user_id, grade_id, schedule_ids,
                                            is_teacher, reason_code, type_code, remark,
                                            extend_info, start_date, end_date, new_start_date,
                                            new_end_date, last_modified_date);
                            List<wlx_schedule_change> wlxScheduleChanges = WLXGreenDaoUtil
                                    .getScheduleChangeDao
                                            ().queryBuilder().where(wlx_schedule_changeDao
                                            .Properties
                                            .Schedule_change_id.eq(schedule_change_id)).list();
                            if (wlxScheduleChanges.size() > 0) {
                                if (last_modified_date > wlxScheduleChanges.get(0)
                                        .getLast_modified_date()) {
                                    WLXGreenDaoUtil.getScheduleChangeDao().update
                                            (wlxSchuduleChange);
                                }
                            } else {
                                WLXGreenDaoUtil.getScheduleChangeDao().insert(wlxSchuduleChange);
                            }

                        }
                    }
                });
    }
}
